package com.yw.swagger.service.serviceImpl;

import com.yw.swagger.mapper.SpNoticeMapper;
import com.yw.swagger.model.SpNotice;
import com.yw.swagger.model.SpNoticeFile;
import com.yw.swagger.service.SpNoticeService;
import com.yw.swagger.tool.Convert;
import com.yw.swagger.tool.Utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * Created by 14267 on 2019/10/24
 */
@Service
public class SpNoticeServiceImpl implements SpNoticeService {

    @Autowired
    private SpNoticeMapper spNoticeMapper;
//    private static final Integer pageLen = 10; //默认页面展示条数

    /**
     * 查询所有通知
     * @return
     */
    @Override
    public List<SpNotice> selectNoticesAllList() {
        return spNoticeMapper.selectNoticesAllList();
    }

    /**
     * 根据条件查询通知
     * @param spNotice
     * @return
     */
    @Override
    public List<SpNotice> selectNoticesList(SpNotice spNotice) {
        return spNoticeMapper.selectNoticesList(spNotice);
    }

    /**
     * 查询收件人未读通知
     * @param spNotice
     * @return
     */
    @Override
    public List<SpNotice> selectNoticesAnswerList(SpNotice spNotice) {
        return spNoticeMapper.selectNoticesAnswerList(spNotice);
    }

    /**
     * 收件人查询单条所有信息
     * @param noticeId
     * @return
     */
    @Override
    public SpNotice selectNoticeInfoByNoticeId(String noticeId,String toUser) {
        return spNoticeMapper.selectNoticeInfoByNoticeId(noticeId,toUser);
    }

    /**
     * 发件人查询单条所有信息
     * @param noticeId
     * @return
     */
    @Override
    public List<SpNotice> selectNoticeFromInfoByNoticeId(String noticeId) {
        return spNoticeMapper.selectNoticeFromInfoByNoticeId(noticeId);
    }


    /**
     * 查询某条通知的所有附件
     * @param noticeId
     * @return
     */
    @Override
    public List<SpNoticeFile> selectNoticeFileByNoticeId(String noticeId){
        return spNoticeMapper.selectNoticeFileByNoticeId(noticeId);
    }

    /**
     * 新增通知
     * @param spNotice
     * @return
     */
    @Override
    public int addNotice(SpNotice spNotice,String toUsers,String lxrs,String lxdhs,String fileNames,String filePath){
        String uuid = UUID.randomUUID().toString();
        String[] us = Convert.toStrArray(toUsers);
        String[] ls = Convert.toStrArray(lxrs);
        String[] lds = Convert.toStrArray(lxdhs);
        String[] fn = Convert.toStrArray(fileNames);
        String[] fp = Convert.toStrArray(filePath);
        for (int i = 0; i < us.length; i++){
            spNotice.setNoticeId(uuid);
            spNotice.setToUser(us[i]);
            spNotice.setLxr(ls[i]);
            spNotice.setLxdh(lds[i]);
            spNotice.setNTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
            spNoticeMapper.addNotice(spNotice);
        }
        for (int i = 0; i < fn.length; i++){
            SpNoticeFile snp = new SpNoticeFile();
            snp.setNoticeId(uuid);
            snp.setFileName(fn[i]);
            snp.setFilePath(fp[i]);
            spNoticeMapper.addNoticeFile(snp);
        }
        return 0;
    }

    /**
     * 收件人删除通知
     * @param noticeId
     * @return
     */
    @Override
    public int delNoticeTo(String noticeId,String toUser){
        return spNoticeMapper.delNoticeTo(noticeId,toUser);
    }

    @Override
    public int deleteByIds(String noticeIds,String toUser){
        String[] Ids = Convert.toStrArray(noticeIds);
        return spNoticeMapper.deleteByIds(Ids,toUser);
    }

    @Override
    public int setAnswer(SpNotice spNotice){
        return spNoticeMapper.setAnswer(spNotice);
    }
}
